# ==============================================================================
# file name: 01-recoding-surveys.R
# authors: Bernhard Clemm / Sjifra de Leeuw
# date: February 18, 2022
# Description: Start with this file to recode the data
# ==============================================================================

# SETUP ========================================================================

basedir <- paste0(dirname(dirname(
  rstudioapi::getSourceEditorContext()$path)), "/")
codedir <- paste0(basedir, "code/")
datadir <- paste0(basedir, "data/")

library(tidyverse)
library(haven)

source(paste0(codedir, "survey-recoding-functions_PL.R"))
source(paste0(codedir, "survey-recoding-functions_US.R"))

# Define DVs ####

dvs <- c("att_str_pre", "att_str_post", 
         "att_imp_pre", "att_imp_post",
         "ft_pre", "ft_post", 
         "stupid_pre", "stupid_post", 
         "under_pre", "under_post", 
         "malvol_pre", "malvol_post", 
         "perpol_pre", "perpol_post",
         "supp_compr_pre", "supp_compr_post",
         "ptc_pre", "ptc_post",
         "know_pre", "know_post",
         "knowfac_pre", "knowfac_post", 
         "well_feel_post", "well_act_post")

# POLAND DATA ==================================================================

pl_w1 <- read_sav(paste0(datadir, "pl_surveys/PL_survey_w1_raw_filtered.sav"))
pl_w2 <- read_sav(paste0(datadir, "pl_surveys/PL_survey_w2_raw_filtered.sav"))
pl_post <- read_sav(paste0(datadir, "pl_surveys/pl_survey_post.sav")) %>%
  left_join(., read.csv(paste0(datadir, "pl_surveys/pl_person_ids.csv")) %>%
              select(person_id, respondent_id)) 

pl_interim01 <- read_sav(paste0(datadir, "pl_surveys/pl_survey_check1.sav")) %>%
  left_join(., read_sav(paste0(datadir, "pl_surveys/pl_survey_check2.sav")), 
            by = "respondent_id") %>%
  left_join(., read_sav(paste0(datadir, "pl_surveys/pl_survey_check3.sav")), 
            by = "respondent_id") %>%
  left_join(., read_sav(paste0(datadir, "pl_surveys/pl_survey_check4.sav")), 
            by = "respondent_id") %>%
  left_join(., read_sav(paste0(datadir, "pl_surveys/pl_survey_check5.sav")), 
            by = "respondent_id") %>%
  left_join(., read_sav(paste0(datadir, "pl_surveys/pl_survey_check6.sav")), 
            by = "respondent_id") %>%
  left_join(., read.csv(paste0(datadir, "pl_surveys/pl_person_ids.csv")) %>%
              select(person_id, respondent_id)) 

## Rename post-survey variables similar to other waves ####

pl_post <- pl_post %>%
  rename("respondent_id_post" = respondent_id,
         "polknow_1_post" = exp6know_r1, 
         "polknow_2_post" = exp6know_r2, 
         "polknow_3_post" = exp6know_r3, 
         "polknow_4_post" = exp6know_r4, 
         "polknow_5_post" = exp6know_r5,
         "polknow_prime_post" = exp9wiedza1,
         "polknow_head_post" = exp9wiedza2,
         "polknow_threshold_post" = exp9wiedza3,
         "polknow_rights_post" = exp9wiedza4,
         "important_woman_post" = exp11qq40_r1,
         "important_religion_post" = exp11qq40_r2, 
         "important_foreign_post" = exp11qq40_r3, 
         "important_econ_post" = exp11qq40_r5, 
         "important_pers_post" = exp11qq40_r6,
         "strong_woman_post" = exp10qq39_r1, 
         "strong_religion_post" = exp10qq39_r2,
         "strong_foreign_post" = exp10qq39_r3, 
         "strong_econ_post" = exp10qq39_r5, 
         "strong_pers_post" = exp10qq39_r6,
         "ft_party_opp_post" = exp16qq50r1, 
         "ft_pis_post" = exp16qq50r4, 
         "ft_sld_post" = exp16qq50r5, 
         "ft_gov_post" = exp16qq50r2,
         "ft_opp_post" = exp16qq50r3,
         "ft_left_post" = exp16qq50r6, 
         "ft_right_post" = exp16qq50r7, 
         "ft_women_post" = exp16qq50r8, 
         "ft_relig_post" = exp16qq50r9, 
         "ft_eu_post" = exp16qq50r10, 
         "ft_econ_post" = exp16qq50r11,
         "ft_welfare_post" = exp16qq50r12,
         "ft_health_post" = exp16qq50r13,
         "ft_crime_post" = exp16qq50r14,
         "ft_climate_post" = exp16qq50r15,
         "ft_lgbt_post" = exp16qq50r16,
         "ft_immigrant_post" = exp16qq50r17,
         "ft_justice_post" = exp16qq50r18,
         "ft_education_post" = exp16qq50r19,
         "stupid_party_opp_post" = exp15p08_h1_r2, 
         "stupid_pis_post" = exp15p08_h1_r5, 
         "stupid_sld_post" = exp15p08_h1_r6, 
         "stupid_gov_post" = exp15p08_h1_r3,
         "stupid_opp_post" = exp15p08_h1_r4,
         "stupid_left_post" = exp15p08_h1_r7, 
         "stupid_right_post" = exp15p08_h1_r8,
         "stupid_econ_post" = exp15p08_h1_r12, 
         "stupid_relig_post" = exp15p08_h1_r10, 
         "stupid_women_post" = exp15p08_h1_r9, 
         "stupid_eu_post" = exp15p08_h1_r11,
         "stupid_welfare_post" = exp15p08_h1_r13,
         "stupid_health_post" = exp15p08_h1_r14,
         "stupid_crime_post" = exp15p08_h1_r15,
         "stupid_climate_post" = exp15p08_h1_r16,
         "stupid_lgbt_post" = exp15p08_h1_r17,
         "stupid_immigrant_post" = exp15p08_h1_r18,
         "stupid_justice_post" = exp15p08_h1_r19,
         "stupid_education_post" = exp15p08_h1_r20,
         "understand_party_opp_post" = exp14qq47_h1_r2,
         "understand_pis_post" = exp14qq47_h1_r5, 
         "understand_sld_post" = exp14qq47_h1_r6,
         "understand_gov_post" = exp14qq47_h1_r3,
         "understand_opp_post" = exp14qq47_h1_r4,
         "understand_left_post" = exp14qq47_h1_r7, 
         "understand_right_post" = exp14qq47_h1_r8,
         "understand_women_post" = exp14qq47_h1_r9, 
         "understand_relig_post" = exp14qq47_h1_r10, 
         "understand_eu_post" = exp14qq47_h1_r11, 
         "understand_econ_post" = exp14qq47_h1_r12,
         "understand_welfare_post" = exp14qq47_h1_r13,
         "understand_health_post" = exp14qq47_h1_r14,
         "understand_crime_post" = exp14qq47_h1_r15,
         "understand_climate_post" = exp14qq47_h1_r16,
         "understand_lgbt_post" = exp14qq47_h1_r17,
         "understand_immigrant_post" = exp14qq47_h1_r18,
         "understand_justice_post" = exp14qq47_h1_r19,
         "understand_education_post" = exp14qq47_h1_r20,
         "malvol_gov_1_post" = exp21qq58zw_1_r2, 
         "malvol_gov_2_post" = exp21qq58zw_1_r3, 
         "malvol_gov_3_post" = exp21qq58zw_1_r4, 
         "malvol_gov_4_post" = exp21qq58zw_1_r5, 
         "malvol_gov_5_post" = exp21qq58zw_1_r6,
         "malvol_opp_1_post" = exp22qq58op_1_r2, 
         "malvol_opp_2_post" = exp22qq58op_1_r3, 
         "malvol_opp_3_post" = exp22qq58op_1_r4, 
         "malvol_opp_4_post" = exp22qq58op_1_r5, 
         "malvol_opp_5_post" = exp22qq58op_1_r6,
         "supp_compr_1_post" = exp17compromise1_r1, 
         "supp_compr_2_post" = exp18compromise2_r1, 
         "supp_compr_3_post" = exp19compromise3_r1, 
         "supp_compr_4_post" = exp20compromise4_r1,
         "perpol_1_post" = exp23qq58_2_r2, 
         "perpol_2_post" = exp23qq58_2_r3, 
         "perpol_3_post" = exp23qq58_2_r4,
         "perpol_4_post" = exp23qq58_2_r5,
         "well_act_1_post" = exp5_r1, 
         "well_act_2_post" = exp5_r2, 
         "well_act_3_post" = exp5_r3,  
         "well_act_4_post" = exp5_r4,  
         "well_act_5_post" = exp5_r5,
         "well_feel_1_post" = exp4_r1, 
         "well_feel_2_post" = exp4_r2, 
         "well_feel_3_post" = exp4_r3, 
         "well_feel_4_post" = exp4_r4, 
         "well_feel_5_post" = exp4_r5, 
         "well_feel_6_post" = exp4_r6,
         "part_petition_post" = exp24particip_r1,
         "part_donate_post" = exp24particip_r2,
         "part_share_post" = exp24particip_r3,
         "part_hearing_post" = exp24particip_r4,
         "part_demo_post" = exp24particip_r5,
         "part_contact_post" = exp24particip_r6,
         "part_volunt_post" = exp24particip_r7,
         "part_media_post" = exp24particip_r8,
         "news_more_1_post" = exp1,
         "news_more_2_post" = exp2,
         "days_week_tv_post" = exp3q8_h1_r1,
         "days_week_print_post" = exp3q8_h1_r2,
         "days_week_radio_post" = exp3q8_h1_r3,
         "days_week_web_post" = exp3q8_h1_r4,
         "days_week_sm_post" = exp3q8_h1_r5,
         "days_week_msg_post" = exp3q8_h1_r6,
         "days_week_f2f_post" = exp3q8_h1_r7,
         "days_week_app_post" = exp3q8_h1_r8,
         "days_week_search_post" = exp3q8_h1_r9)

## Join waves ####

pl_survey01 <- recode_join_PL(wave1 = pl_w1, wave2 = pl_w2) %>%
  left_join(., pl_post, by = "person_id") %>%
  # only keep respondents that were in W2
  filter(!is.na(respondent_id_w2)) 

# POLAND RECODING ==============================================================

## Sociodemographics ####
pl_survey02 <- pl_survey01 %>%
  mutate(gender_cat = case_when(
    gender_w1 == 1 ~ "Male", 
    gender_w1 == 2 ~ "Female"),
    age_cat = case_when(
      age_cat_w1 == 2 ~ "18-24",
      age_cat_w1 == 3 ~  "25-34",
      age_cat_w1 == 4 ~ "35-44",
      age_cat_w1 == 5 ~  "45-54",
      age_cat_w1 == 6 ~ "55+"),
    edu_cat = case_when(
      edu_w1 %in% c(1) ~ "podstawowe",
      edu_w1 %in% c(2) ~ "zasadnicze zawodowe",
      edu_w1 %in% c(3, 4) ~ "średnie",
      edu_w1 %in% c(5,6) ~ "wyższe"),
    edu_high = ifelse(edu_w1 < 5, "Low", "High"))
  
## Treatment ####
pl_survey03 <- pl_survey02 %>%
  mutate(condition = case_when(
    exp_ctl_w2 %in% c(1, 2) ~ "More News - Control",
    exp_treat_w2 %in% c(1, 2) ~ "More News - Experiment"),
  # table(pl_survey03$condition, exclude = NULL)
  # Control: 500; Experiment: 476
  consented = case_when(
    exp_ctl_consent_w2 %in% c(1, 2) ~ "More News - Control",
    exp_treat_consent_w2 %in% c(1, 2) ~ "More News - Experiment")) %>%
  # table(pl_survey03$consented, exclude = NULL)
  # Control: 494 - 6; Experiment: 445 - 31
  mutate(condition_num = case_when(
    condition == "More News - Control" ~ 0,
    condition == "More News - Experiment" ~ 1))
  
## Left-right position #### 
pl_survey04 <- pl_survey03 %>%
  mutate(
    # Not using the "personally important issue", as it differs across people
    leftright_att_w2 = select(., c(
      "att_econ_1_w2", "att_econ_2_w2", "att_econ_3_w2",
      "att_women_1_w2", "att_women_2_w2", "att_women_3_w2",
      "att_relig_1_w2", "att_relig_2_w2", "att_relig_3_w2",
      "att_eu_1_w2", "att_eu_2_w2", "att_eu_3_w2")) %>%
      rowMeans(na.rm = TRUE)) %>%
  mutate(
    # Left-right defined first by government opposition/support
    leftright_w2 = case_when(
      gov_suppopp_w2 < 5 ~ "left",
      gov_suppopp_w2 > 5 ~ "right"),
    # ... if neutral, by support for left or right party
    leftright_w2 = case_when(
      is.na(leftright_w2) & party_w2 %in% c(5, 6, 7, 8, 9) ~ "left",
      is.na(leftright_w2) & party_w2 %in% c(1, 2, 3, 4) ~ "right",
      TRUE ~ as.character(leftright_w2)),
    # ... if neutral, by left-right ideology
    leftright_w2 = case_when(
      is.na(leftright_w2) & ideo_w2 < 5 ~ "left",
      is.na(leftright_w2) & ideo_w2 > 5 ~ "right",
      TRUE ~ as.character(leftright_w2)),
    # ... if neutral, by left-right attitude index
    leftright_w2 = case_when(
      is.na(leftright_w2) & leftright_att_w2 < 6 ~ "left",
      is.na(leftright_w2) & leftright_att_w2 > 6 ~ "right",
      TRUE ~ as.character(leftright_w2))) %>%
  mutate(
    # Left-right defined first by ideology
    leftright_ideo_w2 = case_when(
      ideo_w2 < 5 ~ "left",
      ideo_w2 > 5 ~ "right"),
    # ... if neutral, by government support/opposition
    leftright_ideo_w2 = case_when(
      is.na(leftright_ideo_w2) & gov_suppopp_w2 < 5 ~ "left",
      is.na(leftright_ideo_w2) & gov_suppopp_w2 > 5 ~ "right",
      TRUE ~ as.character(leftright_ideo_w2)),
    # ... if neutral, by support for left or right party
    leftright_ideo_w2 = case_when(
      is.na(leftright_ideo_w2) & party_w2 %in% c(5, 6, 7, 8, 9) ~ "left",
      is.na(leftright_ideo_w2) & party_w2 %in% c(1, 2, 3, 4) ~ "right",
      TRUE ~ as.character(leftright_ideo_w2)),
    # ... if neutral, by left-right attitude index
    leftright_ideo_w2 = case_when(
      is.na(leftright_ideo_w2) & leftright_att_w2 < 6 ~ "left",
      is.na(leftright_ideo_w2) & leftright_att_w2 > 6 ~ "right",
      TRUE ~ as.character(leftright_ideo_w2)))

## Prior self-reported news exposure ####
## PAP: "In wave 1, we asked respondents about the number of days in the last 
## week in which they have gotten information about politics nd current events 
## from 9 media (television, newspapers and magazines, radio, etc.) on a scale 
## from 0 to 7 days. In wave 2... we ask them what has changed since the prior 
## survey so that we can update these numbers."  
pl_survey05 <- pl_survey04 %>%
  mutate(
    days_week_tv_w2 = ifelse(
      days_week_tv_w2 == 99, days_week_tv_w1, days_week_tv_w2),
    days_week_print_w2 = ifelse(
      days_week_print_w2 == 99, days_week_print_w1, days_week_print_w2),
    days_week_radio_w2 = ifelse(
      days_week_radio_w2 == 99, days_week_radio_w1, days_week_radio_w2),
    days_week_web_w2 = ifelse(
      days_week_web_w2 == 99, days_week_web_w1, days_week_web_w2),
    days_week_sm_w2 = ifelse(
      days_week_sm_w2 == 99, days_week_sm_w1, days_week_sm_w2),
    days_week_msg_w2 = ifelse(
      days_week_msg_w2 == 99, days_week_msg_w1, days_week_msg_w2),
    days_week_f2f_w2 = ifelse(
      days_week_f2f_w2 == 99, days_week_f2f_w1, days_week_f2f_w2),
    days_week_app_w2 = ifelse(
      days_week_app_w2 == 99, days_week_app_w1, days_week_app_w2),
    days_week_search_w2 = ifelse(
      days_week_search_w2 == 99, days_week_search_w1, days_week_search_w2)) %>%
  # PAP: "...Finally, we use this information to build a self-reported measure 
  # of prior news exposure by adding up for each individual the numeric responses 
  # for these 9 questions."
  mutate(news_self = rowSums(
           select(., c(days_week_tv_w2, days_week_print_w2, days_week_radio_w2,
                       days_week_web_w2, days_week_sm_w2, days_week_search_w2, 
                       days_week_msg_w2, days_week_f2f_w2, days_week_app_w2)), na.rm = T)) %>%
  # PAP: "... after flipping the scale for [leftists], we use this score to measure their prior levels of like-minded exposure."
  mutate(news_like_self = ifelse(
    leftright_w2 == "left", newsdiet_ideo_w2*(-1) + 8, newsdiet_ideo_w2))

## Attitude strength ####
pl_survey06 <- pl_survey05 %>%
  mutate(att_str_pre = rowMeans(
    select(., intersect(starts_with("strong_"), ends_with("w2"))), na.rm = T),
    att_str_post = rowMeans(
      select(., intersect(starts_with("strong_"), ends_with("post"))), na.rm = T))

## Attitude importance ####
pl_survey07 <- pl_survey06 %>%
  mutate(att_imp_pre = rowMeans(
    select(., intersect(starts_with("important_"), ends_with("w2"))), na.rm = T),
    att_imp_post = rowMeans(
      select(., intersect(starts_with("important_"), ends_with("post"))), na.rm = T))

## Affective polarization ####
## "respondents are asked about their feelings towards 5 types of groups: 
## a) supporters of the party they feel 'farthest from', 
## b) supporters of the largest party on the other political side, 
## c) those who hold opposing stance on the government (supporters versus opponents), 
## d) out-ideologues, and e) those who hold opposing views on the five issues. 
## \item \textit{Measures}: respondents are asked about their feelings towards 
## these groups in three different ways: a) using a feeling thermometer (0-100 range), 
## b) reporting how much they understand each out-group (0-100 range), and 
## c) indicating the extent to which they consider members of the out-group to be stupid (7-point scale)
pl_survey08 <- pl_survey07 %>%
  mutate(
    # Invert all feeling thermometers
    across(starts_with("ft_"), ~ . *(-1) + 100),
    # FT/stupid/understand for government opponents/supporters
    ft_govopp_w2 = case_when(
      leftright_w2 == "left" ~ ft_gov_w2,
      leftright_w2 == "right" ~ ft_opp_w2),
    ft_govopp_post = case_when(
      leftright_w2 == "left" ~ ft_gov_post,
      leftright_w2 == "right" ~ ft_opp_post),
    stupid_govopp_w2 = case_when(
      leftright_w2 == "left" ~ stupid_gov_w2,
      leftright_w2 == "right" ~ stupid_opp_w2),
    stupid_govopp_post = case_when(
      leftright_w2 == "left" ~ stupid_gov_post,
      leftright_w2 == "right" ~ stupid_opp_post),
    under_govopp_w2 = case_when(
      leftright_w2 == "left" ~ understand_gov_w2,
      leftright_w2 == "right" ~ understand_opp_w2),
    under_govopp_post = case_when(
      leftright_w2 == "left" ~ understand_gov_post,
      leftright_w2 == "right" ~ understand_opp_post),
    # FT/stupid/understand for PiS/SLD
    ft_pissld_w2 = case_when(
      leftright_w2 == "left" ~ ft_pis_w2,
      leftright_w2 == "right" ~ ft_sld_w2),
    ft_pissld_post = case_when(
      leftright_w2 == "left" ~ ft_pis_post,
      leftright_w2 == "right" ~ ft_sld_post),
    ## stupid_pis_w2, stupid_sld_w2 not in the data!
    under_pissld_w2 = case_when(
      leftright_w2 == "left" ~ understand_pis_w2,
      leftright_w2 == "right" ~ understand_sld_w2),
    under_pissld_post = case_when(
      leftright_w2 == "left" ~ understand_pis_post,
      leftright_w2 == "right" ~ understand_sld_post),
    # FT/stupid/understand for left-right
    ft_ideo_opp_w2 = case_when(
      leftright_ideo_w2 == "left" ~ ft_right_w2,
      leftright_ideo_w2 == "right" ~ ft_left_w2),
    ft_ideo_opp_post = case_when(
      leftright_ideo_w2 == "left" ~ ft_right_post,
      leftright_ideo_w2 == "right" ~ ft_left_post),
    ## stupid_left_w2, stupid_right_w2 not in the data!
    under_ideo_opp_w2 = case_when(
      leftright_ideo_w2 == "left" ~ understand_right_w2,
      leftright_ideo_w2 == "right" ~ understand_left_w2),
    under_ideo_opp_post = case_when(
      leftright_ideo_w2 == "left" ~ understand_right_post,
      leftright_ideo_w2 == "right" ~ understand_left_post)) %>%
  # Create one measure for "people who disagree on [personally important issue]"
  mutate(
    ft_pers_w2 = case_when(
      pers_issue_w1 == 1 ~ ft_welfare_w2,
      pers_issue_w1 == 2 ~ ft_health_w2,
      pers_issue_w1 == 3 ~ ft_crime_w2,
      pers_issue_w1 == 4 ~ ft_climate_w2,
      pers_issue_w1 == 5 ~ ft_lgbt_w2,
      pers_issue_w1 == 6 ~ ft_immigrant_w2,
      pers_issue_w1 == 7 ~ ft_justice_w2,
      pers_issue_w1 == 8 ~ ft_education_w2),
    ft_pers_post = case_when(
      pers_issue_w1 == 1 ~ ft_welfare_post,
      pers_issue_w1 == 2 ~ ft_health_post,
      pers_issue_w1 == 3 ~ ft_crime_post,
      pers_issue_w1 == 4 ~ ft_climate_post,
      pers_issue_w1 == 5 ~ ft_lgbt_post,
      pers_issue_w1 == 6 ~ ft_immigrant_post,
      pers_issue_w1 == 7 ~ ft_justice_post,
      pers_issue_w1 == 8 ~ ft_education_post),
    stupid_pers_w2 = case_when(
      pers_issue_w1 == 1 ~ stupid_welfare_w2,
      pers_issue_w1 == 2 ~ stupid_health_w2,
      pers_issue_w1 == 3 ~ stupid_crime_w2,
      pers_issue_w1 == 4 ~ stupid_climate_w2,
      pers_issue_w1 == 5 ~ stupid_lgbt_w2,
      pers_issue_w1 == 6 ~ stupid_immigrant_w2,
      pers_issue_w1 == 7 ~ stupid_justice_w2,
      pers_issue_w1 == 8 ~ stupid_education_w2),
    stupid_pers_post = case_when(
      pers_issue_w1 == 1 ~ stupid_welfare_post,
      pers_issue_w1 == 2 ~ stupid_health_post,
      pers_issue_w1 == 3 ~ stupid_crime_post,
      pers_issue_w1 == 4 ~ stupid_climate_post,
      pers_issue_w1 == 5 ~ stupid_lgbt_post,
      pers_issue_w1 == 6 ~ stupid_immigrant_post,
      pers_issue_w1 == 7 ~ stupid_justice_post,
      pers_issue_w1 == 8 ~ stupid_education_post),
    understand_pers_w2 = case_when(
      pers_issue_w1 == 1 ~ understand_welfare_w2,
      pers_issue_w1 == 2 ~ understand_health_w2,
      pers_issue_w1 == 3 ~ understand_crime_w2,
      pers_issue_w1 == 4 ~ understand_climate_w2,
      pers_issue_w1 == 5 ~ understand_lgbt_w2,
      pers_issue_w1 == 6 ~ understand_immigrant_w2,
      pers_issue_w1 == 7 ~ understand_justice_w2,
      pers_issue_w1 == 8 ~ understand_education_w2),
    understand_pers_post = case_when(
      pers_issue_w1 == 1 ~ understand_welfare_post,
      pers_issue_w1 == 2 ~ understand_health_post,
      pers_issue_w1 == 3 ~ understand_crime_post,
      pers_issue_w1 == 4 ~ understand_climate_post,
      pers_issue_w1 == 5 ~ understand_lgbt_post,
      pers_issue_w1 == 6 ~ understand_immigrant_post,
      pers_issue_w1 == 7 ~ understand_justice_post,
      pers_issue_w1 == 8 ~ understand_education_post)) %>%
  # Create index measures
  mutate(
    ft_pre = select(., c(
      ft_party_opp_w2, ft_govopp_w2, ft_pissld_w2, ft_ideo_opp_w2, 
      ft_women_w2, ft_relig_w2, ft_eu_w2, ft_econ_w2, ft_pers_w2)) %>%
      rowMeans(na.rm = T),
    ft_post = select(., c(
      ft_party_opp_post, ft_govopp_post, ft_pissld_post, ft_ideo_opp_post, 
      ft_women_post, ft_relig_post, ft_eu_post, ft_econ_post, ft_pers_post)) %>%
      rowMeans(na.rm = T),
    stupid_pre = select(., c(
      stupid_party_opp_w2, stupid_govopp_w2, 
      stupid_women_w2, stupid_relig_w2, stupid_eu_w2, stupid_econ_w2, stupid_pers_w2)) %>%
      rowMeans(na.rm = T),
    stupid_post = select(., c(
      stupid_party_opp_post, stupid_govopp_post, 
      stupid_women_post, stupid_relig_post, stupid_eu_post, stupid_econ_post, stupid_pers_post)) %>%
      rowMeans(na.rm = T),
    under_pre = select(., c(
      understand_party_opp_w2, under_govopp_w2, under_pissld_w2, under_ideo_opp_w2, 
      understand_women_w2, understand_relig_w2, understand_eu_w2, understand_econ_w2, understand_pers_w2)) %>%
      rowMeans(na.rm = T),
    under_post = select(., c(
      understand_party_opp_post, under_govopp_post, under_pissld_post, under_ideo_opp_post, 
      understand_women_post, understand_relig_post, understand_eu_post, understand_econ_post, understand_pers_post)) %>%
      rowMeans(na.rm = T))

## Attribution of malevolence ####
## PAP: "...by averaging responses for these 5 items."
pl_survey09 <- pl_survey08 %>%
  mutate(malvol_1_post = case_when(
    leftright_w2 == "left" ~ malvol_gov_1_post,
    leftright_w2 == "right" ~ malvol_opp_1_post),
    malvol_2_post = case_when(
      leftright_w2 == "left" ~ malvol_gov_2_post,
      leftright_w2 == "right" ~ malvol_opp_2_post),
    malvol_3_post = case_when(
      leftright_w2 == "left" ~ malvol_gov_3_post,
      leftright_w2 == "right" ~ malvol_opp_3_post),
    malvol_4_post = case_when(
      leftright_w2 == "left" ~ malvol_gov_4_post,
      leftright_w2 == "right" ~ malvol_opp_4_post),
    malvol_5_post = case_when(
      leftright_w2 == "left" ~ malvol_gov_5_post,
      leftright_w2 == "right" ~ malvol_opp_5_post),
    malvol_1_w2 = case_when(
      leftright_w2 == "left" ~ malvol_gov_1_w2,
      leftright_w2 == "right" ~ malvol_opp_1_w2),
    malvol_2_w2 = case_when(
      leftright_w2 == "left" ~ malvol_gov_2_w2,
      leftright_w2 == "right" ~ malvol_opp_2_w2),
    malvol_3_w2 = case_when(
      leftright_w2 == "left" ~ malvol_gov_3_w2,
      leftright_w2 == "right" ~ malvol_opp_3_w2),
    malvol_4_w2 = case_when(
      leftright_w2 == "left" ~ malvol_gov_4_w2,
      leftright_w2 == "right" ~ malvol_opp_4_w2),
    malvol_5_w2 = case_when(
      leftright_w2 == "left" ~ malvol_gov_5_w2,
      leftright_w2 == "right" ~ malvol_opp_5_w2)) %>%
  mutate(across(c(malvol_4_w2, malvol_5_w2, 
                  malvol_4_post, malvol_5_post), ~ . *(-1) + 8)) %>%
  mutate(malvol_pre = rowMeans(
    select(., paste0("malvol_", 1:5, "_w2")), na.rm = T),
    malvol_post = rowMeans(
      select(., paste0("malvol_", 1:5, "_post")), na.rm = T))

## Perceived polarization ####
pl_survey10 <- pl_survey09 %>%
  mutate(perpol_pre = rowMeans(
    select(., paste0("perpol_", 1:4, "_w2")), na.rm = T),
    perpol_post = rowMeans(
      select(., paste0("perpol_", 1:4, "_post")), na.rm = T))

## Support for compromise ####
## PAP: "We will average their responses to these items to measure support for compromise."
pl_survey11 <- pl_survey10 %>%
  mutate(supp_compr_pre = rowMeans(
    select(., paste0("supp_compr_", 1:4, "_w2")), na.rm = T),
    supp_compr_post = rowMeans(
      select(., paste0("supp_compr_", 1:4, "_post")), na.rm = T))

## Self-reported knowledge ####
pl_survey12 <- pl_survey11 %>%
  # PAP: "after recoding the reverse worded items"
  mutate(across(c(polknow_2_post, polknow_4_post, polknow_5_post,
                  polknow_2_w2, polknow_4_w2, polknow_5_w2), ~ .*(-1) + 8)) %>%
  # "... These 7-point scales (from strongly disagree to strongly agree) will be averaged"
  mutate(know_pre = rowMeans(
    select(., paste0("polknow_", 1:5, "_w2")), na.rm = T),
    know_post = rowMeans(
    select(., paste0("polknow_", 1:5, "_post")), na.rm = T))

## Actual knowledge ####
pl_survey13 <- pl_survey12 %>%
  mutate(
    knowfac_1_post = case_when(
     polknow_prime_post == 3 ~ 1, 
     polknow_prime_post %in% c(1, 2, 4) ~ 0),
   knowfac_2_post = case_when(
     polknow_prime_post == 1 ~ 1, 
     polknow_prime_post %in% c(2, 3, 4) ~ 0),
   knowfac_3_post = case_when(
     polknow_prime_post == 3 ~ 1, 
     polknow_prime_post %in% c(1, 2, 4) ~ 0),
   knowfac_4_post = case_when(
     polknow_prime_post == 1 ~ 1, 
     polknow_prime_post %in% c(2, 3, 4) ~ 0),
   knowfac_1_w2 = case_when(
     polknow_prime_w2 == 3 ~ 1, 
     polknow_prime_w2 %in% c(1, 2, 4) ~ 0),
   knowfac_2_w2 = case_when(
     polknow_prime_w2 == 1 ~ 1, 
     polknow_prime_w2 %in% c(2, 3, 4) ~ 0),
   knowfac_3_w2 = case_when(
     polknow_prime_w2 == 3 ~ 1, 
     polknow_prime_w2 %in% c(1, 2, 4) ~ 0),
   knowfac_4_w2 = case_when(
     polknow_prime_w2 == 1 ~ 1, 
     polknow_prime_w2 %in% c(2, 3, 4) ~ 0)) %>%
  mutate(knowfac_pre = rowMeans(
    select(., paste0("knowfac_", 1:4, "_w2")), na.rm = T),
    knowfac_post = rowMeans(
        select(., paste0("knowfac_", 1:4, "_post")), na.rm = T))

## Participation ####
pl_survey14 <- pl_survey13 %>%
  mutate(ptc_pre = rowSums(
    across(c(intersect(starts_with("part_"), ends_with("w2"))), na.rm = TRUE)),
    ptc_post = rowSums(
      across(c(intersect(starts_with("part_"), ends_with("post"))), na.rm = TRUE)))

## Well-being ####
pl_survey15 <- pl_survey14 %>%
  mutate(across(c(paste0("well_feel_", 3:6, "_post")), ~ .*(-1) + 8)) %>%
  mutate(well_feel_post = select(., starts_with("well_feel_")) %>%
           rowMeans(na.rm = T)) %>%
  mutate(well_act_post = select(., starts_with("well_act_")) %>%
           rowMeans(na.rm = T))

## Compliance ####
## We just create index for whether subjects reports more/less consumption

## PAP: "For each interim survey, respondents who report they consumed 
## \textit{more} news in response to question 1 receive a score of 1, and 
## respondents who report using more mediums for news consumption in response to 
## question 2 also receive a score of 1. Thus, across all interim surveys, 
## a respondent who reports perfect compliance will have a score of 12, 
## and a respondent who reports being completely non-compliant will have a score of 0."

pl_interim <- pl_interim01 %>%
  mutate(across(c(starts_with("y1eksp"), starts_with("y2eksp")),
         ~ case_when(. == 1 ~ 1, . == 2 ~ 0))) %>%
  mutate(news_self_interim = select(., c(starts_with("y1eksp"), 
                                         starts_with("y2eksp"))) %>%
           rowMeans(na.rm = T)) %>%
  select(respondent_id, person_id, 
         c(starts_with("y1eksp"), starts_with("y2eksp")), news_self_interim)

## PAP: " Furthermore, we ask both the control and treatment group additional 
## compliance questions in the post-survey, including whether - in the past week 
## - they have used information media more often, less often, or as often as usual. 
## We assign a score of 0 for "less often," 1 for "as often as usual," and 2 for 
## "more often". Likewise, we ask them if they have used fewer, more, or about the same number 
## of mediums for news consumption in the last week, giving a score of 0 for 
## "fewer", 1 for "as many as usual", and 2 for "more". Finally, the post-test 
## also asks about the number of days in the last week respondents have gotten 
## information about politics and current events from 9 mediums on a scale from 
## 0 to 7. Those who report 0 days receive a score of 0 for each medium, those 
## who report 1 day for 1 medium receive a score of 1, and so forth. These 
## self-reports (six from six short surveys, and one at the post-survey) are 
## then added, yielding a value of 79 for full compliance on all of these measures.
## NOTE: DIFFERENCE TO PAP (AVERAGE BETWEEN 0 - 1 INSTEAD OF SUM)

pl_survey16 <- pl_survey15 %>%
  left_join(., pl_interim) %>%
  mutate(across(starts_with("news_more_"), 
                # 1: more often; 2: less often; 3: more or less same
                ~ case_when(. == 2 ~ 0, . == 3 ~ 0.5, . == 1 ~ 1)),
         across(intersect(starts_with("days_week_"), ends_with("_post")),
                # recode days per week into a scale of 0 - 1
                ~ ((. - 0) / (7 - 0)))) %>%
  mutate(news_self_post = 
           select(., c(starts_with("news_more_"), 
                       intersect(starts_with("days_week_"), ends_with("_post")))) %>% 
           rowMeans(na.rm = T)) %>%
  mutate(news_self_intpost = select(., c(news_self_post, news_self_interim)) %>% rowMeans(na.rm = T)) %>%
  # Compliance is simply the self-reported news change (only from post-survey)
  mutate(compl_self_post = news_self_post)

write.csv(pl_survey16, paste0(datadir, "processed/survey_pl.csv"), row.names = F)

## Rescaling 0-100 ####
# FT are already on that scale
# Factual knowledge has scale 0 - 1
# Participation has scale 0 - 9
# Well act 1 - 8
# All others have scale 1 - 7

pl_survey <- pl_survey16 %>%
  mutate(across(
    all_of(dvs[!dvs %in% c(
      "ft_pre", "ft_post", "knowfac_pre", "knowfac_post", "well_act_post")]),
    ~ ((. - 1) / (7 - 1))*100)) %>%
  mutate(well_act_post = ((well_act_post - 1) / (8 - 1))*100) %>%
  mutate(across(c("knowfac_pre", "knowfac_post"), ~ ((. - 0) / (1 - 0))*100)) 

rm(list = c(paste0("pl_survey0", 1:9), 
            paste0("pl_survey", 10:16)))

# US DATA ======================================================================
  
us_w0a <- read.csv(paste0(datadir, "us_surveys/US_survey_w0_lucid_raw.csv"))
us_w0b <- read.csv(paste0(datadir, "us_surveys/US_survey_w0_qualtrics_raw.csv"))
us_w1 <- read.csv(paste0(datadir, "us_surveys/US_survey_w1_raw.csv"))
us_w3 <- read.csv(paste0(datadir, "us_surveys/US_survey_w3_raw.csv"))
missing_sociodem <- read.csv(paste0(datadir, "us_surveys/us_missing_w0_sociodems.csv"))
us_post <- read.csv(paste0(datadir, "us_surveys/US_postsurvey_raw.csv")) %>%
  rename("respondent_id_post" = ResponseId)

us_interim_01 <- read.csv(paste0(datadir, "us_surveys/US_checkin_1.csv"))[-c(1:2),] %>%
  select(RecordedDate, external_id, ppl, media) %>%
  mutate(across(c(ppl, media), ~ case_when(. == "Not at all" ~ 1,
                                           . == "Somewhat" ~ 2,
                                           . == "Mostly" ~ 3,
                                           . == "Fully" ~ 4)))
us_interim_00 <- read.csv(paste0(datadir, "us_surveys/US_checkin_2.csv"))[-c(1:2),] %>%
  select(RecordedDate, external_id, ppl, media)

us_survey_00 <- recode_join_US(
  wave1 = us_w1, wave3 = us_w3,
  wave0a = us_w0a, wave0b = us_w0b, 
  w1_missing_sociodem = missing_sociodem) %>%
  left_join(., us_post %>% select(-person_id), by = "ResponseId_w1") %>% 
  # post-survey:
  ## two people that could not be matched, but also missing on all vars
  ## one person that took survey twice (R_3LZMXvfq4xpNJNf); second participation excluded from sample
  filter(!is.na(ResponseId_w3))

# US RECODING ==================================================================

## Sociodemographics ####
us_survey_01 <- us_survey_00 %>%
  mutate(gender_cat = gender_w0_fac,
    age_cat = case_when(
      age_w0 < 25 ~ "18-24",
      age_w0 >= 25 & age_w0 < 35 ~ "25-34",
      age_w0 >= 35 & age_w0 < 45 ~ "35-44",
      age_w0 >= 45 & age_w0 < 55 ~ "45-54",
      age_w0 >= 55 & age_w0 < 65 ~ "55-64",
      age_w0 >= 65 ~ "65+"),
    edu_cat = case_when(
      edu_w0 %in% c(1, 5, 6) ~ "Less than high school",
      edu_w0 %in% c(8) ~ "High school graduate",
      edu_w0 %in% c(10) ~ "Completed some college",
      edu_w0 %in% c(9, 11) ~ "Associate Degree / vocational",
      edu_w0 %in% c(12, 13) ~ "Bachelor",
      edu_w0 %in% c(14, 15) ~ "Master's / Doctoral degree"),
    edu_high = ifelse(edu_w0 %in% c(1, 5, 6, 8, 9, 10, 11), "Low", "High"))

## Treatment ####
us_survey_02 <- us_survey_01 %>%
  mutate(
    condition = case_when(
      embed_nonews_condition_w3 == "control" ~ "No News - Control",
      embed_nonews_condition_w3 == "experiment" ~ "No News - Experiment"), 
    # table(us_survey_02$condition, exclude = NULL)
    ## Experiment: 513; Control: 359
    invited = case_when(
    !is.na(nonews_ctl_w3) ~ "No News - Control",
    !is.na(nonews_treat_w3) ~ "No News - Experiment"),
    # table(us_survey_02$invited, exclude = NULL)
    ## Experiment: 508; Control: 350; Missing: 14 (these dropped out during survey)
    accepted = case_when(
      nonews_ctl_w3 == 1 | nonews_treat_w3 == 1 ~ "Accepted",
      nonews_ctl_w3 == 0 | nonews_treat_w3 == 0 ~ "Not accepted"),
    # table(us_survey_02$condition, us_survey_02$accepted, exclude = NULL)
    # Experiment: 459 - 49; Control: 346 - 4; Missing: 14
    consented = case_when(
      nonews_ctl_consent_w3 == 1 | nonews_treat_consent_w3 == 1 ~ "Consented",
      nonews_ctl_consent_w3 == 0 | nonews_treat_consent_w3 == 0 ~ "Not consented")) %>%
    # table(us_survey_02$condition, us_survey_02$consented, exclude = NULL)
    # Experiment: 457 - 1; Control: 346; Missing: 68 
  mutate(condition_num = case_when(
    condition == "No News - Control" ~ 0,
    condition == "No News - Experiment" ~ 1))

## Left-right position #### 
us_survey_03 <- us_survey_02 %>%
  mutate(
    leftright_att_w3 = select(., c(
      "att_econ_1_w3", "att_econ_2_w3", "att_econ_3_w3",
      "att_climate_1_w3", "att_climate_2_w3", "att_climate_3_w3",
      "att_immigrant_1_w3", "att_immigrant_2_w3", "att_immigrant_3_w3", 
      "att_gun_1_w3", "att_gun_2_w3", "att_gun_3_w3")) %>%
      rowMeans(na.rm = TRUE)) %>%
  # Left-right partisanship
  mutate(
    # ... first by party support
    leftright_w3 = case_when(
      party_w3 < 4 ~ "left",
      party_w3 > 4 ~ "right"),
    # ... if neutral, by party W0
    leftright_w3 = case_when(
      is.na(leftright_w3) & party_w0 < 4 ~ "left",
      is.na(leftright_w3) & party_w0 > 4 ~ "right",
      TRUE ~ as.character(leftright_w3)),
    # ... if neutral, by ideo W3
    leftright_w3 = case_when(
      is.na(leftright_w3) & ideo_w3 < 5 ~ "left",
      is.na(leftright_w3) & ideo_w3 > 5 ~ "right",
      TRUE ~ as.character(leftright_w3)),
    # ... if neutral, by ideo W0
    leftright_w3 = case_when(
        is.na(leftright_w3) & ideo_w0 < 5 ~ "left",
        is.na(leftright_w3) & ideo_w0 > 5 ~ "right",
      TRUE ~ as.character(leftright_w3)),
    # ... if neutral, by Trump support
    leftright_w3 = case_when(
      is.na(leftright_w3) & approve_trump_w1 < 4 ~ "left",
      is.na(leftright_w3) & approve_trump_w1 > 4 ~ "right",
      TRUE ~ as.character(leftright_w3)),
    # ... if neutral, by left-right attitude index
    leftright_w3 = case_when(
      is.na(leftright_w3) & leftright_att_w3 < 6 ~ "left",
      is.na(leftright_w3) & leftright_att_w3 > 6 ~ "right",
      TRUE ~ as.character(leftright_w3))) %>%
  # Left-right ideology
  mutate(
    # ... first by party support
    leftright_ideo_w3 = case_when(
      ideo_w3 < 5 ~ "left",
      ideo_w3 > 5 ~ "right"),
    # ... if neutral, by ideo W1
    leftright_ideo_w3 = case_when(
      is.na(leftright_ideo_w3) & ideo_w0 < 5 ~ "left",
      is.na(leftright_ideo_w3) & ideo_w0 > 5 ~ "right",
      TRUE ~ as.character(leftright_ideo_w3)),
    # ... if neutral, by party W3
    leftright_ideo_w3 = case_when(
      is.na(leftright_ideo_w3) & party_w3 < 4 ~ "left",
      is.na(leftright_ideo_w3) & party_w3 > 4 ~ "right",
      TRUE ~ as.character(leftright_ideo_w3)),
    # ... if neutral, by ideo W3
    leftright_ideo_w3 = case_when(
      is.na(leftright_ideo_w3) & party_w0 < 5 ~ "left",
      is.na(leftright_ideo_w3) & party_w0 > 5 ~ "right",
      TRUE ~ as.character(leftright_ideo_w3)),
    # ... if neutral, by Trump support
    leftright_ideo_w3 = case_when(
      is.na(leftright_ideo_w3) & approve_trump_w1 < 4 ~ "left",
      is.na(leftright_ideo_w3) & approve_trump_w1 > 4 ~ "right",
      TRUE ~ as.character(leftright_ideo_w3)),
    # ... if neutral, by left-right attitude index
    leftright_ideo_w3 = case_when(
      is.na(leftright_ideo_w3) & leftright_att_w3 < 6 ~ "left",
      is.na(leftright_ideo_w3) & leftright_att_w3 > 6 ~ "right",
      TRUE ~ as.character(leftright_ideo_w3)))

## Prior self-reported news exposure ####
## PAP: "In the pre survey we also ask respondents about the number of days in 
# the last week in which they have gotten information about politics and current 
# events from 9 media (television, newspapers and magazines, radio, etc.) 
# on a scale from 0 to 7 days. We use this information to build a self-reported
# measure of prior news exposure by adding up for each individual the numeric 
# responses for these 10 questions. The variable will range from 0 to 63, 
# with higher values meaning higher levels of prior news consumption/exposure."  
us_survey_04 <- us_survey_03 %>%
  mutate(news_self = rowSums(
    select(., c(days_week_tv_w3, days_week_print_w3, days_week_radio_w3,
                days_week_web_w3, days_week_sm_w3, days_week_search_w3, 
                days_week_msg_w3, days_week_f2f_w3, days_week_app_w3)), na.rm = T)) %>%
  # PAP: "... After ``flipping'' the scale for self-reported [Democrats]... 
  # we use this score to measure their prior levels of like-minded exposure."
  mutate(news_like_self = ifelse(
    leftright_w3 == "left", newsdiet_ideo_w3*(-1) + 8, newsdiet_ideo_w3))

## Attitude strength ####
# PAP: "(6 issues): respondents are asked to report how strong are their views 
# on 6 policy issues (7-point scale)"
us_survey_05 <- us_survey_04 %>%  
  mutate(att_str_pre = rowMeans(
    select(., c("strong_gun_w3", "strong_immigrant_w3", "strong_climate_w3",
                "strong_trump_w3", "strong_econ_w3", "strong_pers_w3")), na.rm = T),
    att_str_post = rowMeans(
      select(., c("ISSTRONG_1", "ISSTRONG_2", "ISSTRONG_3",
                  "ISSTRONG_4", "ISSTRONG_5", "ISSTRONG_6")), na.rm = T))

## Attitude importance ####
# PAP: "(6 issues): respondents are asked to report how important 
# the same issues are to them (7-point scale)"
us_survey_06 <- us_survey_05 %>%
  mutate(att_imp_pre = rowMeans(
    select(., c("important_gun_w3", "important_immigrant_w3", "important_climate_w3",
                "important_trump_w3", "important_econ_w3", "important_pers_w3")), na.rm = T),
    att_imp_post = rowMeans(
      select(., c("ISPERSO_1", "ISPERSO_2", "ISPERSO_3",
                  "ISPERSO_4", "ISPERSO_5", "ISPERSO_6")), na.rm = T))

## Affective polarization ####
## PAP: "respondents are asked about their feelings towards 3 types of groups: 
## a) out-partisans, b) out-ideologues, and 
## c) those who hold opposing views on 2 of the 6 issues. 
## respondents are asked about their feelings towards these 3 types of groups in
## 3 different ways: a) using a feeling thermometer (0-100 range), 
## b) reporting how much they understand each out group (0-100 range), and 
## c) saying the extent to which they consider members of the out group to be stupid (7-point scale)."
us_survey_07 <- us_survey_06 %>%
  mutate(
    # Invert all feeling thermometers
    across(c(starts_with("ft_"), starts_with("THERMOM_")),  ~ . *(-1) + 100),
    # FT for Republicans/Democrats
    ft_outparty_w3 = case_when(
      leftright_w3 == "left" ~ ft_rep_supporters_w3,
      leftright_w3 == "right" ~ ft_dem_supporters_w3),
    ft_outparty_post = case_when(
      leftright_w3 == "left" ~ THERMOM_5,
      leftright_w3 == "right" ~ THERMOM_4),
    # FT for Liberals/conservatives
    ft_outideo_w3 = case_when(
      leftright_ideo_w3 == "left" ~ ft_conservatives_w3,
      leftright_ideo_w3 == "right" ~ ft_liberals_w3),
    ft_outideo_post = case_when(
      leftright_ideo_w3 == "left" ~ THERMOM_7,
      leftright_ideo_w3 == "right" ~ THERMOM_6)) 

### For stupid/understand items, recoding more complicated bc of Independents
us_survey_08 <- us_survey_07 %>%
  
  mutate(
    # Stupid W3
    ## a) out-party
    stupid_outparty_w3 = case_when(
      leftright_w3 == "right" & embed_op_party_nameW3_w3 == "Democratic Party" ~ stupid_party_opp_w3, 
      leftright_w3 == "left" & embed_op_party_nameW3_w3 == "Republican Party" ~ stupid_party_opp_w3,
      leftright_w3 == "right" & embed_op_party_nameW3_w3 == "Republican Party" ~ stupid_ind_dem_w3),
    # b) out-ideo - DOES NOT EXIST!
    # c) issues: stupid_gun_w3, ... stupid_econ_w3, stupid_pers_w3
    # Understand W3
    # a) out-party
    under_outparty_w3 = ifelse(
      # not using Independents because error in survey: Inds asked about Reps and Inds
      party_w3 != 4, understand_party_opp_w3, NA),
    # b) out-ideo 
    under_outideo_w3 = case_when(
      leftright_ideo_w3 == "right" & embed_op_ideoW3_w3 == "liberal" ~ understand_ideo_opp_w3, 
      leftright_ideo_w3 == "left" & embed_op_ideoW3_w3 == "conservative" ~ understand_ideo_opp_w3,
      leftright_ideo_w3 == "left" & embed_op_ideoW3_w3 == "liberal" ~ understand_ideo_w3)) %>%
    # c) issues: understand_gun_w3, ... understand_econ_w3, understand_pers_w3
  
  mutate(
    # Stupid Post
    # a) out-party
    stupid_outparty_post = ifelse(
        # not using Independents because error in survey: Inds asked about Reps and Inds
        party_w3 != 4, STUPID_7, NA),
    # b) out-ideo - DROP BECAUSE NOT EXISTING IN W3
    # c) issues: no recoding needed: STUPID_1, ... STUPID_6
    # Understand post
    # a) out-party 
    under_outparty_post = ifelse(
      # not using Independents because error in survey: Inds asked about Reps and Inds
      party_w3 != 4, UNDERSTAND_7, NA),
    # b) out-ideo 
    under_outideo_post = case_when(
      leftright_ideo_w3 == "right" & op_ideoW3 == "liberal" ~ UNDERSTAND_9, 
      leftright_ideo_w3 == "left" & op_ideoW3 == "conservative" ~ UNDERSTAND_9,
      leftright_ideo_w3 == "left" & ideo_nameW3 == "liberal" ~ UNDERSTAND_10))
  # c) issues: UNDERSTAND_1 ... UNDERSTAND_6

us_survey_09 <- us_survey_08 %>%
  mutate(
    ft_pre = select(., c(
      ft_outparty_w3, ft_outideo_w3, 
      ft_gun_w3, ft_immigrant_w3, ft_climate_w3, ft_econ_w3, ft_pers_w3)) %>%
      rowMeans(na.rm = T),
    ft_post = select(., c(
      ft_outparty_post, ft_outideo_post, 
      THERMOM_9, THERMOM_10, THERMOM_11, THERMOM_12, THERMOM_13)) %>%
      rowMeans(na.rm = T),
    stupid_pre = select(., c(
      stupid_outparty_w3,
      stupid_gun_w3, stupid_immigrant_w3, stupid_climate_w3, 
      stupid_econ_w3, stupid_trump_w3, stupid_pers_w3)) %>%
      rowMeans(na.rm = T),
    stupid_post = select(., c(
      stupid_outparty_post, 
      STUPID_1, STUPID_2, STUPID_3, 
      STUPID_4, STUPID_5, STUPID_6)) %>%
      rowMeans(na.rm = T),
    under_pre = select(., c(
      under_outparty_w3, under_outideo_w3,
      understand_gun_w3, understand_immigrant_w3, understand_climate_w3, 
      understand_econ_w3, understand_trump_w3, understand_pers_w3)) %>%
      rowMeans(na.rm = T),
    under_post = select(., c(
      under_outparty_post, under_outideo_post,
      UNDERSTAND_1, UNDERSTAND_2, UNDERSTAND_3, 
      UNDERSTAND_4, UNDERSTAND_5, UNDERSTAND_6)) %>%
      rowMeans(na.rm = T))

## Attribution of malevolence ####
## PAP: "...by averaging responses for these 5 items."

us_survey_10 <- us_survey_09 %>%
  mutate(across(c(malvol_4_w3, malvol_5_w3, MALVOL_4, MALVOL_5), ~ . *(-1) + 8)) %>%
  # first create indices
  mutate(
    malvol_pre = rowMeans(
      select(., paste0("malvol_", 1:5, "_w3")), na.rm = T),
    malvol_ind_pre = rowMeans(
      select(., paste0("malvol_ind_", 1:5, "_w3")), na.rm = T),
    malvol_post = rowMeans(
      select(., paste0("MALVOL_", 1:5)), na.rm = T),
    malvol_ind_post = rowMeans(
      select(., paste0("MALVOL_OWN_", 1:5)), na.rm = T)) %>%
  # now assign correct value to indpendents
  mutate(
    malvol_pre = case_when(
      leftright_w3 == "left" & embed_op_partyW3_w3 == "Republican" ~ malvol_pre,
      leftright_w3 == "right" & embed_op_partyW3_w3 == "Democrat" ~ malvol_pre,
      leftright_w3 == "right" & embed_op_partyW3_w3 == "Republican" ~ malvol_ind_pre),
    malvol_post = case_when(
      leftright_w3 == "left" & op_partyW3 == "Republican" ~ malvol_post,
      leftright_w3 == "right" & op_partyW3 == "Democrat" ~ malvol_post,
      leftright_w3 == "right" & op_partyW3 == "Republican" ~ malvol_ind_post))

## Perceived polarization ####
# PAP: "We will  measure \textit{perceived polarization} by averaging 
# responses to these [4] items."
us_survey_11 <- us_survey_10 %>%
  mutate(perpol_pre = rowMeans(
    select(., paste0("perpol_", 1:4, "_w3")), na.rm = T),
    perpol_post = rowMeans(
      select(., paste0("PERPOL_", 1:4)), na.rm = T))

## Support for compromise ####
## PAP: "We will average their responses to these items to measure \textit{support for compromise}."
us_survey_12 <- us_survey_11 %>%
  mutate(supp_compr_pre = rowMeans(
    select(., paste0("supp_compr_", 1:4, "_w3")), na.rm = T),
    supp_compr_post = rowMeans(
      select(., paste0("COMPROMISE_", 1:4)), na.rm = T))

## Self-reported knowledge ####
us_survey_13 <- us_survey_12 %>%
  # PAP: "after recoding the reverse worded items"
  mutate(across(c(KNOW_2, KNOW_4, KNOW_5), ~ .*(-1) + 8)) %>%
  # "... These 7-point scales (from strongly disagree to strongly agree) will be averaged"
  mutate(know_post = rowMeans(
      select(., paste0("KNOW_", 1:5)), na.rm = T))

## Actual knowledge ####
us_survey_14 <- us_survey_13 %>%
  mutate(
    polknow_pres_post = ifelse(times_elect == 2, 1, 0),
    polknow_majority_post = ifelse(house_party == 1, 1, 0),
    polknow_senator_post = ifelse(senate_term == 6, 1, 0),
    polknow_spend_post = ifelse(spend_least == 1, 1, 0)) %>%
  mutate(knowfac_pre = rowMeans(
    select(., c(polknow_pres_w3, polknow_majority_w3,
                polknow_senator_w3, polknow_spend_w3)), na.rm = T),
    knowfac_post = rowMeans(
      select(., c(polknow_pres_post, polknow_majority_post,
                  polknow_senator_post, polknow_spend_post)), na.rm = T))

## Participation ####
us_survey_15 <- us_survey_14 %>%
  mutate(ptc_pre = rowSums(
    across(c(intersect(starts_with("part_"), ends_with("w3"))), na.rm = TRUE)),
    ptc_post = sapply(strsplit(us_survey_14$PAR_2020, ","), length))

## Well-being ####
us_survey_16 <- us_survey_15 %>%
  mutate(across(c(paste0("WELL_FEELINGS_", 3:6)), ~ .*(-1) + 8)) %>%
  mutate(well_feel_post = select(., starts_with("WELL_FEELINGS_")) %>%
           rowMeans(na.rm = T)) %>%
  mutate(well_act_post = select(., starts_with("WELL_ACT")) %>%
           rowMeans(na.rm = T))

## Compliance ####
## We just create index for whether subjects reports more/less consumption

### Interim surveys

us_interim_10 <- rbind(us_interim_00, us_interim_01) %>%
  mutate(RecordedDate = as.POSIXct(RecordedDate))

us_interim_11 <- us_interim_10 %>%
  select(RecordedDate, external_id, ppl, media) %>%
  arrange(external_id, RecordedDate) %>%
  unique() %>%
  filter(external_id != "") %>%
  group_by(external_id) %>% 
  mutate(count = n(), number = 1:n()) 

us_interim_12 <- us_interim_11 %>%
  mutate(day = as.Date(RecordedDate)) %>%
  arrange(RecordedDate) %>% group_by(external_id, day) %>%
  mutate(number_day = 1:n()) %>%
  # some people have multiple participations on the same day - take first one only
  filter(number_day == 1) %>% select(-number_day) %>%
  group_by(external_id) %>% 
  mutate(count = n(), number = 1:n())

us_interim_13 <- us_interim_12 %>%
  select(external_id, number, media, ppl) %>%
  pivot_wider(names_from = "number", values_from = c("ppl", "media")) %>%
  ungroup() %>%
  mutate(across(c(starts_with("ppl_"), starts_with("media_")), ~ as.numeric(.)),
         across(c(starts_with("ppl_"), starts_with("media_")), ~ (. - 1) / (4 - 1))) %>%
  # average of all compliance measures
  mutate(news_self_interim = select(., c(starts_with("ppl_"), starts_with("media_"))) %>%
           rowMeans(na.rm = T))

### Post-compliance
### PAP: "We ask both the control and treatment groups whether - in the past week 
### - they have (a) tuned into news and (b) talked about politics and current 
### events with  family, friends, and / or acquaintances more often, less often, 
### or about the same as usual. Again, we assign a score of 0 for "less often," 
### 1 for "about the same," and 2 for "more often," with a score 0 for full 
### compliance, and 4 for non-compliance. Finally, the posttest also asks about 
### the number of days in the last week respondents have gotten information about
### politics and current events from 10 media on a scale from 0 to 7. 
### Those who report 0 days, receive a score of 0 for each medium, those who 
### report 1 day for 1 medium, receive a score of 1, and so forth."

us_survey_17 <- us_survey_16 %>%
  left_join(., us_interim_13, by = c("ResponseId_w1" = "external_id")) %>%
  mutate(across(starts_with("DAYSNEWS_"), ~ (. - 1) / (8 - 1)),
         across(c("media_past_week", "discuss_past_week"), 
                ~ case_when(. == 1 ~ 1, . == 3 ~ 0.5, . == 2 ~ 0))) %>%
  mutate(news_self_post = select(., c(media_past_week, discuss_past_week, 
                                      starts_with("DAYSNEWS_"))) %>%
           rowMeans(na.rm = T)) %>%
  mutate(news_self_intpost = select(., c(news_self_post, news_self_interim)) %>% rowMeans(na.rm = T))  %>%
  # Compliance is simply the self-reported news change (only from post-survey), 
  # but flipped because treatment was meant to induce "no news"
  mutate(compl_self_post = news_self_post * (-1) + 1)

## Rescaling 0-100 ####
# FT are already on that scale
# Factual knowledge has scale 0 - 1
# well_act_post has scale 1 - 8
# All others have scale 1 - 7
# know_pre does not exist in US

write.csv(us_survey_17, paste0(datadir, "processed/survey_us.csv"), row.names = F)

us_survey <- us_survey_17 %>%
  mutate(across(
    all_of(dvs[!dvs %in% c(
      "ft_pre", "ft_post", "know_pre", "knowfac_pre", "knowfac_post", "well_act_post")]),
    ~ ((. - 1) / (7 - 1))*100)) %>%
  mutate(well_act_post = ((well_act_post - 1) / (8 - 1))*100) %>%
  mutate(across(c("knowfac_pre", "knowfac_post"), ~ ((. - 0) / (1 - 0))*100)) 

rm(list = c(paste0("us_survey_0", 0:9), 
            paste0("us_survey_", 10:17)))
  
# BIND AND EXPORT ==============================================================

vars <- c("person_id", "respondent_id_w2", "ResponseId_w3", "respondent_id_post",
          "gender_cat", "age_cat", "edu_cat", "edu_high",
          "condition", "condition_num", "consented", "invited", "accepted",
          dvs, paste0(dvs, "_stand"), "leftright_w2", "leftright_w3", 
          "news_self", "news_like_self",
          "news_more_1_post", "news_more_2_post",
          "news_self_intpost", "news_self_post", "news_self_interim", "compl_self_post")

# Bind PL and US
survey_all <- rbind(
  pl_survey %>%
    select(any_of(vars)) %>%
    mutate(country = "PL",
           leftright_w3 = NA,
           ResponseId_w3 = NA_character_,
           invited = NA_character_,
           accepted = NA_character_),
  us_survey %>%
    select(any_of(vars)) %>%
    mutate(country = "US",
           respondent_id_w2 = NA_character_,
           leftright_w2 = NA,
           news_more_1_post= NA, 
           news_more_2_post= NA,
           know_pre = NA)) %>%
  mutate(across(everything(), ~ ifelse(is.nan(.), NA, .)),
         leftright = case_when(
           country == "PL" ~ leftright_w2,
           country == "US" ~ leftright_w3)) %>%
  select(-c(leftright_w2, leftright_w3))

# Export
write.csv(survey_all, paste0(datadir, "processed/survey_all.csv"), row.names = F)

